//cd "C:\Users\creto\Desktop\chinese_students_2017\Submissions\ReStat\Replication Files 2022"
global path = "C:\Users\creto\Desktop\chinese_students_2017\"

*global path "C:\Users\Kevin Shih\Dropbox\Work\Research Projects\Active\chinese_students_2017"

clear
set more off

cap log close


*load data
	use "$path/Submissions/ReStat/Replication Files 2022/kswxy_data.dta", clear
	
	gen sample = 1 if balanced==1 & popwt03!=. 
	egen minsample = min(sample), by(citycode)
	drop sample
	rename minsample sample

	drop if citycode==.
	
	gen exports = dvalue/10000
	
	gen gdppc_true = gdppc*10
	
	gen stud_ot = stud_1 + stud_2 + stud_7 + stud_8
	gen stud_nonfunded = stud_tot - stud_univ_funded
	gen gdp_true = gdp*10000
	gen ln_gdp_true=ln(gdp_true)
	
	foreach x in as ba ma dr ot stem ss arts bus hi_admrate_p0_25 hi_admrate_p25_50 hi_admrate_p50_75 hi_admrate_p75_100 univ_funded nonfunded {
	gen sh_`x' = stud_`x'/stud_tot
	}
	


*set controls
global controls "chinatariff00_expw97 share_revenue_exw97 inputtariff02_expw97 contract_cons_exw97" 
label var contract_cons_exw97 "Contract Intensity"
label var share_revenue_exw97 "Export License"
label var chinatariff00_expw97 "Import Tariffs"
label var inputtariff02_expw97 "Input Tariffs"
label var ln_chinatariff_expw97 "Annual Import Tariffs"
label var ln_inputtariff_expw97 "Annual Input Tariffs"
	

*calculation for interquartile effect	
	sum iv_ntr if year==2013 & balanced==1, de
	local iqr_ivntr = r(p75) - r(p25)
	eststo clear

	
***TABLE 5 ***********
	
	**NEW UHS ANALYSIS RENTS, INCOME PERCENTILES, N HOUSES ETC
		merge m:1 chinacity using "$path/Data/1.ChinaData/UHS/UHS_d_0207_combined"
		drop if _merge==2
		drop _merge
	
		eststo clear

		 //PANEL A
	
	
		eststo w_gdp2: reg lnc_gdp_0213 iv_ntr $controls if year==2013 & balanced==1 & shc_stud_tot_0213!=., cluster(citycode)
		eststo w_pop2: reg lnc_oldpop_0213 iv_ntr $controls if year==2013 & balanced==1 & shc_stud_tot_0213!=., cluster(citycode)
		eststo w_gdppc2: reg lnc_gdppc_0213 iv_ntr $controls if year==2013 & balanced==1 & shc_stud_tot_0213!=., cluster(citycode)
		eststo A2: reg d_sh_afford_tuition iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		
		*convert gdp from in 10,000s to in billions
		gen gdpb = gdp/100000
		sum gdpb if year==2002 & balanced==1
		local ymean: di %12.2f r(mean) 
		estadd local y02mean= `ymean': w_gdp2 
		di `ymean'
		
		*convert pop from in 10,000s to in millions
		gen oldpopb = oldpop/100
		sum oldpopb if year==2002 & balanced==1
		local ymean: di %12.2f r(mean) 
		estadd local y02mean= `ymean': w_pop2
		
		sum gdppc if year==2002 & balanced==1
		local ymean: di %12.0fc r(mean) 
		estadd local y02mean= "`ymean'": w_gdppc2
		
		sum sh_afford_tuition2002 if year==2013 & balanced==1
		local ymean: di %12.3f r(mean) 
		estadd local y02mean= `ymean': A2
		
		
	//Panel B
		eststo clear

	eststo Z2: reg d_ln_inc_housing_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode)
		sum inc_housing_avg2002 if year==2013 & balanced==1
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = `ymean': Z2
		
		eststo A2: reg d_ln_hrent_value_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 		
		sum hrent_value_avg2002 if year==2013 & balanced==1
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = `ymean': A2
		
		eststo B2: reg d_sh_collect_rent iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum sh_collect_rent2002 if year==2013 & balanced==1
		local ymean: di %12.3f r(mean)
		estadd local y02mean = `ymean': B2
		
		eststo C2: reg d_sh_rent_grand_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum sh_rent_grand_avg2002 if year==2013 & balanced==1
		local ymean: di %12.3f r(mean)
		estadd local y02mean = `ymean': C2
		
		eststo D2: reg d_ln_hprice iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum hprice_avg2002 if year==2013 & balanced==1
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = `ymean': D2
		
		eststo E2: reg lnc_ppsq_comm_0213 iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum ppsq_comm if year==2002 & balanced==1
		local ymean: di %12.3fc r(mean)
		estadd local y02mean = `ymean': E2
		
	//Panel C
	eststo clear
	
	eststo B2: reg d_ln_inc_lab_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum inc_lab_avg2002 if balanced==1 & year==2013
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = "`ymean'": B2
		
		eststo C2: reg d_ln_inc_bus_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode)
		sum inc_bus_avg2002 if balanced==1 & year==2013
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = `ymean': C2
		
		eststo D2: reg d_ln_inc_cap iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum inc_cap_avg2002 if balanced==1 & year==2013
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = `ymean': D2
		
		eststo E2: reg d_ln_inc_trans iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 
		sum inc_trans_avg2002 if balanced==1 & year==2013
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = "`ymean'": E2
		
		eststo F2: reg d_ln_credit_avg iv_ntr $controls if year==2013 & balanced==1, cluster(citycode) 		
		sum credit_avg2002 if balanced==1 & year==2013
		local ymean: di %12.0fc r(mean)
		estadd local y02mean = "`ymean'": F2
		
		
***TABLE 6 ***********

//Panel A

		merge m:1 citycode using "$path/Data/6.AnalysisData/IV1_skill/gapsexpweighted97_skill"
		drop if _merge==2
		drop _merge
		
	
		replace ntrgap_expw97_sk_CHN04_med  = 0 if ntrgap_expw97_sk_CHN04_med ==.
		replace ntrgap_expw97_unsk_CHN04_med  = 0 if ntrgap_expw97_unsk_CHN04_med ==.
			
	
eststo Ariel_CHN_med: reg shc_stud_tot_0213 ntrgap_expw97_sk_CHN04_med ntrgap_expw97_unsk_CHN04_med sharetot_expw97_sk_CHN04_med $controls if year==2013 & balanced==1, cluster(citycode)
eststo Ariel_IND_med: reg shc_stud_tot_0213 ntrgap_expw97_sk_IND96_med ntrgap_expw97_unsk_IND96_med sharetot_expw97_sk_IND96_med $controls if year==2013 & balanced==1, cluster(citycode)


//Panel B

		sort citycode year
				*construct network in 2000 as size of student population from each city in 2000
			gen network2000= stud_tot*(year==2000)
			egen stud_tot2000 = max(network2000), by(citycode)
			drop network2000
			
			*interact with ntr instrument
			gen iv_ntr_x_stud_tot2000 = iv_ntr*stud_tot2000
			
			bys citycode: gen stud_tot_2000_2003 = stud_tot[_n-13] + stud_tot[_n-12] +  stud_tot[_n-11] +  stud_tot[_n-10] if year==2013
				gen iv_ntr_x_stud_tot_2000_2003 = iv_ntr * stud_tot_2000_2003
				
				sum iv_ntr if year==2013 & balanced==1, de
				local iqr_ivntr = r(p75) - r(p25)
				
				
eststo B: reg shc_stud_tot_0213 iv_ntr iv_ntr_x_stud_tot2000 stud_tot2000 $controls if year==2013 & balanced==1 , cluster(citycode)
				estadd ysumm
				estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
				
eststo D: reg shc_stud_tot_0213 iv_ntr iv_ntr_x_stud_tot_2000_2003 stud_tot_2000_2003 $controls if year==2013 & balanced==1 , cluster(citycode)
				estadd ysumm
				estadd local iqreffect = round(`iqr_ivntr'*_b[iv_ntr]*1000,1)
				
				
				label var stud_tot2000 "Students in 2000"
				label var iv_ntr_x_stud_tot2000 "\$PNTR_{c}\$ X Students in 2000"
				
				label var stud_tot_2000_2003 "Students in 2000-03"
				label var iv_ntr_x_stud_tot_2000_2003 "\$PNTR_{c}\$ X Students in 2000-03"
				

**************** APPENDIX ******************
	
***** Annual variation in Exports*******	
	gen postntr = (year > 2001)
	gen postgap = postntr*iv_ntr
	foreach x in contract_cons_exw97 chinatariff00_expw97 inputtariff02_expw97 share_revenue_exw97 {
	gen post`x' = postntr*`x'
	}
		replace pop = pop/1000 //now in millions
		sum postgap if year==2013 & balanced==1, de
				local iqr_ivntr = r(p75) - r(p25)

 * global controls_exports "num_college num_midschool numstd_college numstd_midschool  ln_chinatariff_expw97 ln_inputtariff_expw97 postcontract_cons_exw97 postshare_revenue_exw97 " 
   global controls_exports "pop ln_chinatariff_expw97 postinputtariff02_expw97 postcontract_cons_exw97 postshare_revenue_exw97 " 
label var postgap "Post*NTRGAP"
label var postcontract_cons_exw97 "Post*Contract"
label var postshare_revenue_exw97 "Post*Export Lic"
label var ln_oldpop "Log Population"
label var pop "Population (millions)"
label var ln_chinatariff_expw97 "Annual Import Tariffs"
label var ln_inputtariff_expw97 "Annual Input Tariffs"
label var postinputtariff02_expw97 "Post*Input Tariffs"

	***** Export Regressions using the Pierce and Schott Diff-in-diff (since we have older data for exports)

**** Table B.1 ****	
	
eststo clear
//Pre-2008
eststo D:	reghdfe ln_dvalue_usa postgap $controls_exports if year<2007 & sample==1, absorb(citycode year) cluster(citycode)
gen dd = e(sample)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)
eststo E:	reghdfe ln_dvalue_europe postgap $controls_exports if year<2007 & dd==1, absorb(citycode year) cluster(citycode)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)
eststo F:	reghdfe ln_dvalue_nonusa postgap $controls_exports  if year<2007 & dd==1, absorb(citycode year) cluster(citycode)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)

//All Years
eststo A:	reghdfe ln_dvalue_usa postgap $controls_exports if sample==1, absorb(citycode year) cluster(citycode)
gen d = e(sample)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)
eststo B:	reghdfe ln_dvalue_europe postgap $controls_exports if d==1, absorb(citycode year) cluster(citycode)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)
eststo C:	reghdfe ln_dvalue_nonusa postgap $controls_exports if d==1, absorb(citycode year) cluster(citycode)
estadd ysumm
estadd local iqreffect = round(`iqr_ivntr'*_b[postgap]*100,1)

drop d dd	
	
	
	***** New Firms and Investment Regressions, PS Specification**
	**** Table B.2 ****	

	eststo clear
	*reghdfe ln_fnumber postgap $controls_exports if sample==1, absorb(citycode year) cluster(citycode)
	*reghdfe ln_fnumber_manuf postgap $controls_exports if sample==1, absorb(citycode year) cluster(citycode)
eststo A:	reghdfe nfirms_rate_manuf postgap $controls_exports if sample==1 , absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
eststo B:	reghdfe nfirms_rate_asip postgap $controls_exports if sample==1 , absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
*eststo C:	reghdfe linvst_longrun postgap $controls_exports if sample==1 & year <2008, absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
eststo C:	reghdfe linvst_tot_rate postgap $controls_exports if sample==1 , absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
eststo D:	reghdfe linvst_equity_rate postgap $controls_exports if sample==1 , absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
gen ddd = e(sample)
eststo E:	reghdfe linvst_lr_rate postgap $controls_exports if sample==1 & ddd==1, absorb(citycode year) cluster(citycode)
estadd local controls "Yes" , replace
	
	
	
		
